//
//  TvSeriesController.m
//  ArandaTest
//
//  Created by Camilo Cadena on 2/18/15.
//  Copyright (c) 2015 Camilo Cadena. All rights reserved.
//

#import "TvSeriesController.h"
#import "AFNetworking.h"
#import "test1HeaderConstants.h"
#import "ServicesStrings.h"
#import "AFHTTPRequestOperation.h"
#import "SeriesObject.h"
#import "SeasonObject.h"

@implementation TvSeriesController

// Request TV Series that matches the search string
-(void)requestSeriesUsingString:(NSString *)query page:(NSString*)page andCompletionBlock:(void(^)(id))completionBlock{
    
    //build request
    NSString *request = [ServicesStrings getUrlFromString:KSERVICE andString:KSEARCH_SHOW];
    NSDictionary *params = @{@"api_key":KAPI_KEY,@"query":query,@"page":page};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [[manager operationQueue] cancelAllOperations];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
    [manager GET:request parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *responseDictionary = [[NSDictionary alloc] initWithDictionary:responseObject];
        NSArray *results = [[NSArray alloc] initWithArray:[responseDictionary valueForKey:@"results"]];
        NSMutableArray *finalResults = [[NSMutableArray alloc] init];
        for(int i=0; i<results.count; i++){
            SeriesObject *item = [SeriesObject instanceFromDictionary:[results objectAtIndex:i]];
            [finalResults addObject:item];
        }
        
        NSDictionary *resultDictionary = @{@"results":finalResults,@"pages":[responseDictionary valueForKey:@"total_pages"]};
        completionBlock(resultDictionary);
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        completionBlock(error);
    }];
}

//Request TV Series Poster Image
-(void)requestCellPosterWithPath:(NSString *)path andCompletionBlock:(void(^)(id))completionBlock{
    
}

//Request all the Seasons for a selected TV Series
-(void)requestAllSeasonsForSelectedSeriesId:(NSString *)seletedShow andCompletionBlock:(void(^)(id))completionBlock{
    
    //build request
    NSString *request = [ServicesStrings getUrlFromString:KSERVICE andString:[NSString stringWithFormat:KSHOW_SEASONS,seletedShow]];
    NSDictionary *params = @{@"api_key":KAPI_KEY};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
    [manager GET:request parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *responseDictionary = [[NSDictionary alloc] initWithDictionary:responseObject];
        NSArray *results = [[NSArray alloc] initWithArray:[responseDictionary valueForKey:@"seasons"]];
        NSMutableArray *finalResults = [[NSMutableArray alloc] init];
        for(int i=0; i<results.count; i++){
            SeasonObject *item = [SeasonObject instanceFromDictionary:[results objectAtIndex:i]];
            [finalResults addObject:item];
        }
        
        completionBlock(finalResults);
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        completionBlock(error);
    }];
    
}


@end
